<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea v-model="msg" placeholder="请输入要评论的内容（最多120字）" maxlength="120"></textarea>

        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
        <div class="cmt-list" v-for="(item, i) in comments" :key="item.id">
            <div class="cmt-item">
                <div class="cmt-title">
                    <span>第{{i+1}}楼&nbsp;&nbsp;用户：{{item.username}}</span>
                    <span>发表时间：{{item.add_time}}</span>
                </div>
                <div class="cmt-body">
                    {{item.content === 'undefined' ? '此用户很懒，嘛都没说' : item.content}}
                </div>
            </div>
        </div>
        

        <mt-button type="primary" size="large" plain @click="getMore">加载更多</mt-button>
    </div>
</template>
<script>
import Mock from 'mockjs'
import {Toast} from 'mint-ui'
export default {
    data(){
        return{
            pageIndex:1,
            comments:[],//所有评论数据
            msg:'',
        }
    },
    created(){
        this.getComments()
    },
    methods:{
        getComments(){
            this.$http.get('http://rapapi.org/mockjs/31926/api/getcomments'+"?pageindex="+this.pageIndex)
            .then(result=>{
                var response = Mock.mock(result.body)
                if(response.status){
                    //this.comments = response.message
                    //每当获取新评论数据的时候，不要把老数据清空覆盖，而是应该以老数据，拼接上新数据
                    this.comments = this.comments.concat(response.message)
                    //console.log(this.comments)
                }
            })
        },
        getMore(){//加载更多
            this.pageIndex++
            this.getComments()
        },
        postComment(){
            if(this.msg.trim().length == 0){
                Toast('评论内容不能为空')
                return
            }
            //发表评论
            //参数1：请求的URL地址
            //参数2：提交给服务器的数据对象{content:this.msg}
            //参数3：定义提交的时候，表单中数据的格式{emulateJSON: true}
            //this.$http.post('api/postcomment/' + this.$route.params.id,{
            // 1.拼接出一个评论对象
            var cmt = {
                user_name: '匿名用户',
                add_time:new Date(),
                content: this.msg.trim()
            }
            this.comments.unshift(cmt)
            this.msg  = ""
            /*
            this.$http.post('api/postcomment',{
                content:this.msg.trim(),
                id:this.$route.params.id
            })
            .then(result=>{
                
                var response = Mock.mock(result.body)
                console.log('response',response)
                if(response.status){
                    
                }
            })
            .catch(err=>{
                console.log('err',err)
            })
            */
        }
    },
    props:['id']
}
</script>
<style lang="scss" scoped>
.cmt-container{
    padding-bottom:10px;
    h3{
        font-size: 18px;
    }
    textarea{
        font-size: 14px;
        height: 85px;
        margin: 0px;
    }
    .cmt-list{
        margin: 5px 0;
        .cmt-item{
            font-size:13px;
            .cmt-title{
                line-height: 30px;
                background-color: #ccc;
                padding:0 5px;
                display: flex;
                justify-content: space-between;
            }
            .cmt-body{
                line-height: 35px;
                text-indent: 2em;
            }
        }
        
    }
}
</style>


